/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto.obs;

import com.facebook.airlift.configuration.AbstractConfigurationAwareModule;
import com.facebook.airlift.log.Logger;
import com.facebook.presto.hive.s3.S3ConfigurationUpdater;
import com.google.inject.Binder;
import com.google.inject.Scopes;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.common.JavaUtils;

import static com.facebook.airlift.configuration.ConfigBinder.configBinder;
import static java.util.Objects.requireNonNull;
import static org.weakref.jmx.ObjectNames.generatedNameOf;
import static org.weakref.jmx.guice.ExportBinder.newExporter;

public class HiveObsModule
        extends AbstractConfigurationAwareModule
{
    private static final Logger log = Logger.get(HiveObsModule.class);

    private static final String EMR_FS_CLASS_NAME = "org.apache.hadoop.fs.obs.EmrFileSystem";

    private final String connectorId;

    public HiveObsModule(String connectorId)
    {
        this.connectorId = requireNonNull(connectorId, "connectorId is null");
    }

    @Override
    protected void setup(Binder binder)
    {
        PrestoObsFileSystemType type = PrestoObsFileSystemType.PRESTO;
        if (type == PrestoObsFileSystemType.PRESTO) {
            binder.bind(S3ConfigurationUpdater.class).to(PrestoObsConfigurationUpdater.class).in(Scopes.SINGLETON);
            configBinder(binder).bindConfig(HiveObsConfig.class);

            binder.bind(PrestoObsFileSystemStats.class).toInstance(PrestoObsFileSystem.getFileSystemStats());
            newExporter(binder).export(PrestoObsFileSystemStats.class).as(generatedNameOf(PrestoObsFileSystem.class, connectorId));
        }else if (type == PrestoObsFileSystemType.EMRFS) {
            validateEmrFsClass();
            binder.bind(S3ConfigurationUpdater.class).to(EmrFsObsConfigurationUpdater.class).in(Scopes.SINGLETON);
        }else if(type == PrestoObsFileSystemType.HADOOP_DEFAULT){
            binder.bind(S3ConfigurationUpdater.class).to(HadoopDefaultConfigurationUpdater.class).in(Scopes.SINGLETON);;
        }else {
            throw new RuntimeException("Unknown file system type: " + type);
        }
    }

    private static void validateEmrFsClass()
    {
        // verify that the class exists
        try {
            Class.forName(EMR_FS_CLASS_NAME, true, JavaUtils.getClassLoader());
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException("EMR File System class not found: " + EMR_FS_CLASS_NAME, e);
        }
    }

    public static class EmrFsObsConfigurationUpdater implements S3ConfigurationUpdater
    {
        @Override
        public void updateConfiguration(Configuration config)
        {
            // re-map filesystem schemes to use the EMR file system
            config.set("fs.obs.impl", EMR_FS_CLASS_NAME);
        }
    }

    public static class HadoopDefaultConfigurationUpdater implements S3ConfigurationUpdater
    {
        @Override
        public void updateConfiguration(Configuration config)
        {
        }
    }

}
